import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";
import { CmHttp } from "./../http-module/http.service";
import { InfoSourceParams } from "./info-list/info-list.component";

@Component({
  // moduleId: module.id,
  selector: 'stock-info',
  templateUrl: 'stock-info.component.html',
  encapsulation: ViewEncapsulation.None,
  styleUrls: [
    "stock-info.component.less"
  ],
  inputs: [
    "stockSource",
    "appendClass"
  ]
})
export class StockInfoComponent implements OnInit {
  infoList = [];
  stockSource: Observable<any>;
  currentType = "";
  clickSubject = new Subject();
  infoListSource: Observable<InfoSourceParams>;
  tabList = [
    {
      symbol: 0,
      text: "公告",
      $type: "notices"
    },
    {
      symbol: 0,
      text: "新闻",
      $type: "news"
    },
    {
      symbol: 0,
      text: "研报",
      $type: "reports"
    }
  ]

  constructor(private http: CmHttp) { }

  ngOnInit() {
    let { stockSource, clickSubject, http } = this;

    this.infoListSource = stockSource.
      merge(clickSubject).
      scan((lastReq, tabOrStock, index) =>
        tabOrStock.symbol === 0 ?
          {
            ...lastReq,
            $type: tabOrStock.$type
          } : {
            ...lastReq,
            ...tabOrStock,
            $type: tabOrStock.$type
          }
      , this.tabList.reduce(first => first))
  }

  switchInfoType(tab) {
    this.currentType = tab.$type;
    this.clickSubject.next(tab);
  }

}